controller.controller('ServerIndexController',['$rootScope','$scope','serverService','clientService','slotService',function($rootScope,$scope,serverService,clientService,slotService){
    (function(){
        var initServers=function(){
            serverService.getServers(function(response){
                if(!response.success) {
                    core.ui.alert.error(response.data);
                    return;
                }

                $scope.servers=response.data;
            });
        };

        $scope.server={};

        /**
         * info
         * @param id
         */
        $scope.server.info=function(id){
            serverService.getInfo(id,function(response){
                $scope.serverInfoData=response.data.replace(/\r\n/g,'<br/>');
                $scope.$apply();

                core.ui.dialog('#serverInfoDialog');
            });
        };

        initServers();
    })();

    (function(){
        var initClients=function(){
            clientService.getClients(function(response){
                if(!response.success) {
                    core.ui.alert.error(response.data);
                    return;
                }

                $scope.clients=response.data;
            });
        };

        $scope.client={};

        /**
         * info
         * @param id
         */
        $scope.client.info=function(id){
            clientService.getInfo(id,function(response){
                $scope.clientInfoData=response.data.replace(/\r\n/g,'<br/>');
                $scope.$apply();

                core.ui.dialog('#clientInfoDialog');
            });
        };

        /**
         * mapping
         * @param id
         */
        $scope.client.showSlotMapping=function(id){
            clientService.getSlotMapping(id,function(response){
                if(objectUtil.isNull(response.data)) {
                    core.ui.alert.warning("没有槽");
                    return;
                }

                $scope.client.slotMappingData=response.data.slots;
                core.ui.dialog("#clientSlotMappingDialog");
            });
        };

        $scope.client.registry=(function(){
            var dialog;

            return {
                showDialog: function () {
                    slotService.getCount(function (response) {
                        $scope.client.registry.clientRegister={
                            client: 'nettyClient',
                            host: '',
                            port: '',
                            connection: '',
                            timeout: '',
                            slotStart: 0,
                            slotEnd: 0
                        };
                        $scope.slotCount=response.data;
                        dialog = core.ui.dialog('#clientRegisterDialog');
                    });
                },
                closeDialog: function () {
                    dialog.close();
                },
                register: function () {
                    clientService.register($scope.client.registry.clientRegister, function (response) {
                        if (!apiUtil.isSuccess(response)) {
                            core.ui.alert.error(response.data);
                            return;
                        }

                        if (objectUtil.isNull($scope.clients)) {
                            $scope.clients = [];
                        }

                        initClients()
                        dialog.close();
                    }, function (response) {
                        core.ui.alert.error(response);
                        dialog.close();
                    });
                },
                unregister: (function () {
                    var dialog;
                    return {
                        clientId: "",
                        targetClientId: "",
                        showConfirmDialog: function (id) {
                            if ($scope.clients.length == 1) {
                                core.ui.alert.warning('不能删除唯一的节点');
                                return;
                            }

                            $scope.clientUnregister.clientId = id;
                            dialog = core.ui.dialog('#clientUnregisterDialog');
                        },
                        closeConfirmDialog: function () {
                            dialog.close();
                        },
                        unregister: function () {
                            if (stringUtil.isBlank($scope.client.registry.targetClientId)) {
                                core.ui.alert.warning('目标节点错误');
                                return;
                            }

                            clientService.unregister($scope.client.registry.clientId, $scope.client.registry.targetClientId, function (response) {
                                if (apiUtil.isSuccess(response)) {
                                    initClients();
                                    core.ui.alert.success('取消注册成功');
                                    dialog.close();
                                }
                                else {
                                    core.ui.alert.error(response.data);
                                }
                            });
                        }
                    }
                })()
            }
        })();

        $scope.client.routing={};

        $scope.client.routing.registry=(function(){
            var dialog=null;

            return {
                showDialog:function(sourceId){
                    $scope.client.routing.registry.routingRegister={
                        client:'nettyClient',
                        host:'',
                        port:'',
                        connection:'',
                        timeout:'',
                        sourceId:sourceId
                    };

                    dialog=core.ui.dialog('#clientRoutingRegisterDialog');
                },
                closeDialog:function(){
                    dialog.close();
                },
                register:function(){
                    clientService.registerRouting($scope.client.routing.registry.routingRegister.sourceId,$scope.client.routing.registry.routingRegister,function(response){
                        if(!apiUtil.isSuccess(response)) {
                            core.ui.alert.error(response.data);
                            return;
                        }

                        initClients()
                        dialog.close();
                    },function(response){
                        core.ui.alert.error(response);
                        dialog.close();
                    });
                },
                unregister:function(sourceId,routingId){
                    clientService.unregisterRouting(sourceId,routingId,function(response){
                        if(!apiUtil.isSuccess(response)) {
                            core.ui.alert.error(response.data);
                            return;
                        }

                        initClients()
                    });
                }
            }
        })();

        initClients();
    })();
}]);